<?php
	namespace App\Providers;

	use Illuminate\Support\ServiceProvider;

	class cProviderVerifyUserVisitPermission
	{

		private static  $m_o_this;
		public function __construct()
		{
			$this->m_helper             = app()->gcHelper						;
			$this->m_redis              = $this->m_helper->gfGetRedis()			;
			$this->m_redis_key_prefix   = 'cProviderVerifyUserVisitPermission_'	;
		}
		public static function createHelpler()
		{
			# create an object
			#
			if(cProviderVerifyUserVisitPermission::$m_o_this==null)
			{
				cProviderVerifyUserVisitPermission::$m_o_this = (new cProviderVerifyUserVisitPermission());
			}

			#
			#
			return cProviderVerifyUserVisitPermission::$m_o_this;
		}

		/* 用户登录验证成功后应该调用此方法生成唯一的 token ，作为单点登录的唯一凭证
		 *
		 *
		 *
		 *
		 */
		public function setUserVisitPermission($user_id,$user_visit_token='',$user_expire_time=null)
		{
			# create token 
			#
			$ar					= [   
											'user_id'                   => $user_id																	,	# user id
											'user_visit_token'          => $user_visit_token														,	# user token
											'user_token_expire_time'    => $user_expire_time														,	# expire time
											'user_login_time'           => $this->m_helper->gfGetCurrentLocalDate()									,	# user login time
								  ];
			$k					= $this->m_redis_key_prefix.$user_id    ;
			$v					= $this->m_helper->gfJsonEncode($ar)	;

			# set value to redis server
			#
			$this->m_redis->set($k,$v);
		}

		/* 校验用户的 token，如果相等说明用户没有重新登录过保持单点登录状态，否则表明用户已经再次登录，token 验证失败。
		 *
		 *
		 *
		 *
		 *
		 */
		public function verifyUserVisitPermissionOk($user_id,$user_visit_token)
		{
			# set default return value to fail status
			#
			$ar					= $this->m_helper->gfApiEchoReturn(array('user_visit_permission' => false),'800.2000','no visit permission');

			# get value from redis server
			#
			$k					= $this->m_redis_key_prefix.$user_id    ;
			$v					= $this->m_redis->get($k)               ;
			 
			# verify
			#
            if($v=='')
            {
                $ar['code']		= '800.2001';	# fail
            }
            else
            {
                $this->m_helper->gfJsonDecode	(
													$v
													,
													function($j) use(&$r,$user_visit_token,&$ar,$k)
													{
														if($j->user_visit_token === $user_visit_token)
														{
															$ar				= $this->m_helper->gfApiEchoReturn(array('user_visit_permission' => true),'800.1000','user visit permission ok');	# verfiy ok 
														}
														else
														{
															$ar['code']     = '800.2002';
															$ar['message']  = "[user key $k ] [token $user_visit_token ] fail";																	# fail
														}
													}
													,
													function($j) use(&$ar)
													{
														$ar['code']			= '800.2003';
														$ar['message']		= "json format verify fail".$j->getOriginJsonString()." ";															# fail
													}
												);
            }

			#
			#
			return $this->m_helper->gfApiEchoReturnEx($ar);
		}
	}
	class provider_verify_user_visit_permission extends ServiceProvider
	{
		/**
		 * Register services.
		 *
		 * @return void
		 */
		public function register()
		{
			$this->app->singleton('gcProviderVerifyUserVisitPermission',function (){return cProviderVerifyUserVisitPermission::createHelpler();});
		}

		/**
		 * Bootstrap services.
		 *
		 * @return void
		 */
		public function boot()
		{
		}
	}
